package com.converse.diamondreader.base;

import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * Author: Leo Liao <lliao@midtronics.com>
 * Date: 02/12/2021 17:22
 * Description:
 */
public abstract class BasicUtilsActivity extends AppCompatActivity {

    private Toast toast;

    protected void showToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    protected void showLog(String msg) {
        Throwable throwable = new Throwable();
        StackTraceElement[] stackTrace = throwable.getStackTrace();
        if (stackTrace.length < 1) {
            return;
        }
        StackTraceElement element = stackTrace[1];
        String className = element.getClassName();
        className = className.substring(className.lastIndexOf(".") + 1);
        int lineNumber = element.getLineNumber();
        String methodName = element.getMethodName();
        StringBuilder sb = new StringBuilder();
        sb.append(className)
                .append("->")
                .append(methodName)
                .append("->line ")
                .append(lineNumber)
                .append(": ")
                .append(msg);
        Log.e("Diamond Reader", sb.toString());

    }
}
